#ifndef HUD_H
#define HUD_H

#include "DXUT.h"
#include "DXUTgui.h"
#include "SDKmisc.h"
#include "Block.h"
#include "Player.h"
#include <map>

class HUDCursor : public CDXUTControl
{
friend class HUDDialog;

public:
                    HUDCursor( HUDDialog* pDialog = NULL );
    virtual         ~HUDCursor();

	virtual void    Render( float fElapsedTime );
protected:
	HUDDialog*		m_pDialog;
private:

};

class HUDLifebar : public CDXUTControl
{
friend class HUDDialog;
private:
	 Player *		m_pPlayer; 
	 float			m_lastHPValue;
	 float			m_lastHPregValue;
	 float			m_lastEPValue;

	 float			m_blink;
public:
                    HUDLifebar( Player * player, HUDDialog* pDialog = NULL );
    virtual         ~HUDLifebar();

	virtual void    Render( float fElapsedTime );
protected:
	HUDDialog*		m_pDialog;
private:

};

class HUDSelected : public CDXUTControl
{
friend class HUDDialog;

public:
                    HUDSelected( HUDDialog* pDialog = NULL );
    virtual         ~HUDSelected();

	virtual void    Render( float fElapsedTime );
protected:
	HUDDialog*		m_pDialog;
private:

};

struct ITEMRENDERER {
	int		x;
	int		y;
	DWORD	id;

	BYTE	count;
	BYTE	type;
};

class  Item;	
struct ItemStruct {
	int		x;
	int		y;
	Item *	item;
	ItemStruct(int _x, int _y, Item * _item) {
		x		= _x;
		y		= _y;
		item	= _item;
	};
	inline bool operator == (const ItemStruct& rhs) const
	{
		return ((x == rhs.x) && (y == rhs.y));
	};
};

class HUDItem : public CDXUTControl
{
friend class HUDInventoryDialog;
protected:


	HUDInventoryDialog*					m_pDialog;
	std::map<BYTE, ItemStruct>			m_Blockpositions;
	std::map<BYTE, ItemStruct>			m_Itempositions;
public:
					HUDItem( HUDInventoryDialog* pDialog = NULL );
/*
	BYTE			addBlock(int x, int y, Block block);
	void			setBlock(BYTE index, int x, int y, Block block);
	BYTE			addItem(int x, int y, DWORD id);
	void			setItem(BYTE index, int x, int y, DWORD id);
*/
	BYTE			addItem(int x, int y, Item * item);
	void			removeItem(int x, int y);
	void			setItem(BYTE index, int x, int y, Item * item);

	virtual void    Render( float fElapsedTime );
};

class Inventory;
class HUDInventory : public CDXUTControl
{
friend class HUDInventoryDialog;
protected:
	HUDInventoryDialog*		m_pDialog;
	Inventory *				m_pInventory;
public:
					HUDInventory( Inventory * pInventory, HUDInventoryDialog* pDialog = NULL );
	virtual void    Render( float fElapsedTime );
			int 	DragItem( int xPos, int yPos, Item ** item);
			int 	DropItem( int xPos, int yPos, Item ** item);
	//void			handleMouse( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos);
};

class HUDDialogResourceManager : public CDXUTDialogResourceManager
{
private:
    // D3D11
    // Shaders
    ID3D11VertexShader* m_pVSRenderBlock11;
    ID3D11PixelShader*	m_pPSRenderBlock11;
	ID3D11SamplerState* m_pSamplerStateBlock11;
public:
	HUDDialogResourceManager();

	void    OnD3D11DestroyDevice();
	HRESULT OnD3D11CreateDevice( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3d11DeviceContext );

	void	EndBlocks11( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, DXUTTextureNode* pTextureNode = NULL, DXUTTextureNode* pTextureNode2 = NULL );
};

class HUDDialog : public CDXUTDialog
{
protected:
	HUDDialogResourceManager*	m_pAdvManager;
public:
	void                Init( HUDDialogResourceManager* pManager, bool bRegisterDialog = true );

	HRESULT             AddCursor( int ID, bool bIsDefault=false, HUDCursor** ppCreated=NULL );
    HRESULT             AddLifebar( Player * player, int ID, int x, int y, int width, int height, bool bIsDefault=false, HUDLifebar** ppCreated=NULL );
	HRESULT             AddSelected( int ID, int x, int y, int width, int height, bool bIsDefault=false, HUDSelected** ppCreated=NULL );
	/*
	HRESULT             AddItem( int ID, HUDItem** ppCreated = NULL );
	HRESULT				AddInventory( Inventory * pInventory, int ID, int x, int y, HUDInventory** ppCreated = NULL );

	HRESULT				DrawBlock11( std::map<BYTE, ItemStruct>* blocks, float fDepth );
	HRESULT				DrawItem11(  std::map<BYTE, ItemStruct>* items,  float fDepth, byte type = 0 );
	HRESULT				DrawItem11(  ItemStruct item,  float fDepth );
	*/
	HRESULT				DrawRelativeSprite11( CDXUTElement* pElement, RECT* prcDest, float fDepth );
};

class HUDInventoryDialog : public CDXUTDialog
{
protected:
	HUDDialogResourceManager*	m_pAdvManager;
	vector<HUDInventory*>		m_pInventuries;

	//Mouse
	bool						m_bLeftButtonDown; 
	bool						m_bRightButtonDown; 
	bool						m_bMiddleButtonDown; 
	bool						m_bSideButton1Down;
	bool						m_bSideButton2Down;

	Item *						m_pItemHold;
public:
	void                Init( HUDDialogResourceManager* pManager, bool bRegisterDialog = true );

	HRESULT             AddItem( int ID, HUDItem** ppCreated = NULL );
	HRESULT				AddInventory( Inventory * pInventory, int ID, int x, int y, HUDInventory** ppCreated = NULL );

	HRESULT				DrawBlock11( std::map<BYTE, ItemStruct>* blocks, float fDepth );
	HRESULT				DrawItem11(  std::map<BYTE, ItemStruct>* items,  float fDepth, byte type = 0 );
	HRESULT				DrawItem11(  ItemStruct item,  float fDepth );
	HRESULT				DrawRelativeSprite11( CDXUTElement* pElement, RECT* prcDest, float fDepth );

	void				handleMouse( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos);
};

#endif